package com.terrier.ui.containers
{
    import com.terrier.ui.events.WxButtonEvent;
    import com.terrier.ui.skins.IWxSkinBase;
    import com.terrier.ui.skins.IWxWindowSkin;

    import flash.geom.Rectangle;
    import com.terrier.ui.controls.WxButton;
    import com.terrier.ui.controls.WxLabelButton;

    /**
     * 窗口组件
     * @author Jarvis.weng
     */
    public class WxWindow extends WxScrollPanel
    {
        /**
         * @private
         */
        protected var closeButton:WxButton;

        /**
         * @private
         */
        protected var titlePanel:WxLabelButton;

        /**
         * 构造函数
         */
        public function WxWindow()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkin(value:IWxSkinBase):void
        {
            super.setSkin(value);

            if (skin != null)
            {
                closeButton.setSkin(skin.closeBtnSkin);
                titlePanel.setSkin(skin.titleSkin);
            }

            updateLayout();
        }

        /**
         * @private
         */
        override protected function initialize():void
        {
            super.initialize();

            closeButton = new WxButton();
            titlePanel = new WxLabelButton();

            addChild(titlePanel);
            addChild(closeButton);

            closeButton.addEventListener(WxButtonEvent.BUTTON_CLICK, closeBtnClickHandler);
        }

        /**
         * @private
         */
        protected function closeBtnClickHandler(e:WxButtonEvent):void
        {
            close();
        }

        /**
         * @private
         */
        override protected function updateLayout():void
        {
            if (skin == null)
            {
                return;
            }

            if (!skin.customLayout)
            {
                titlePanel.width = _width;

                closeButton.x = _width - closeButton.width - 10;
                closeButton.y = titlePanel.y - 10;

                if (skin.bgSkin != null)
                {
                    skin.bgSkin.x = titlePanel.x;
                    skin.bgSkin.y = titlePanel.y + titlePanel.height;
                    skin.width = _width;
                    skin.height = _height - skin.bgSkin.y;
                }
            }

            super.updateLayout();
        }

        override protected function get contentPaneBound():Rectangle
        {
            return new Rectangle(titlePanel.x, titlePanel.y + titlePanel.height,
                                 _width, _height - titlePanel.y - titlePanel.height);
        }

        private function get skin():IWxWindowSkin
        {
            return _skin as IWxWindowSkin;
        }
    }
}
